/*
* Author: Rong Xue CPE301 Design Assignment 1
*/

.DEF STRIG = R16 ; Define store register R16 with varibale STRIG


LDI STRIG, HIGH(RAMEND) ; Setup stack and set the storage starting from RAM_END location
OUT SPH, STRIG
LDI STRIG, LOW(RAMEND)
OUT SPL, STRIG

LDI R19, 0x14 ; store 0x14=20 into R19 which is a counter
LDI R18, 0x32 ; store 0x32=50 into R18 which my 20 numbers will be 50-69


L1: MOV STRIG, R18 ; loop that will push 20 numbers start from 50 to 69 into the stack
PUSH STRIG
INC R18
DEC R19
BRNE L1 ; loop will end when counter become 0

LDI R19, 0x15 ; store 0x15=21 into R19 as counter, here I use 21 because I want to parsing all 20 numbers
LDI R22, 0x00 ; store counter 0 into R22
LDI R20, HIGH(0x0000) ; store initial value for R20:R21
LDI R21, LOW(0x0000)
LDI R23, HIGH(0x0000) ; store initial value for R23:R24
LDI R24, LOW(0x0000)

C35LP: ; loop that will parsing if the number is divisible by 3 and 5
DEC R19 ; decrement counter R19
CP R19, R22 ; check if all 20 numbers have been parsed with counter
BREQ OUTLP ; if all 20 numbers have been parsed, end the loop
POP STRIG ; pop the value from the stack
MOV R15, STRIG ; store the popped value into R15 register

LDI R17, 0x3 ; store 0x3 or number 3 into R17
LDI R25, 0x3 ; store 0x3 into R25 for parsing divisible by 3 purpose

LDI R26, 0x5 ; store 0x5 into R25 for parsing divisible by 3 purpose
LDI R27, 0x5 ; store 0x5 into R25 for parsing divisible by 3 purpose

TMP: ; loop that will compare the nxt value when the parsing done with previous number
CP R17, R15 ; compare the value with R17, which R17 is n times of 3
BRGE C35LP ; if R17 is greater or equal to the value start parsing the next value
ADD R17, R25 ; if not add 3 to R17(which means R17 will always be some power of 3)

ADD R26, R27 ; add the 5 into R26 so that R26 will always be some power of 5

CPSE R17, R15 ; compare the value with R17, if they are equal means this value is divisible by 3
RJMP TMP1 ; if they are not equal go to TMP1 check if it is divisible by 5
ADD R21, R15 ; if they are equal, add this number into R20:R21
TMP1: ; loop for parsing if this value is divisible by 5
CPSE R26, R15 ; compare the value with R26, if they are equal then it is divisible by 5
RJMP TMP ; if they are not euqal go back to TMP loop and increase the power of both 3 and 5
ADD R24, R15 ; if they are equal then this value is divisible by 5 so add this number into R23:R24

CP R19, R22 ; check counter to see if all 20 numbers been parsed
BRNE TMP ; if counter is not equal 0 then go back to the loop again
RJMP OUTLP ; if the counter is equal 0 then end the loop

OUTLP:



RET

